package com.mall507.fivepeaks.widget.ximageupload;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hzh.frame.ui.activity.BaseUI;
import com.hzh.frame.util.FileUtil;
import com.hzh.frame.util.StatusBarUtil;
import com.mall507.comn.ComnConfig;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.util.Util;

import java.io.File;

public class XimageUploadActivity extends BaseUI implements View.OnClickListener{

    private Animation into,remove;
	private LinearLayout context;
	
	private int width=610 ,height=960;
	
	private static final String IMAGE_FILE_NAME = "/UserIDCard.jpg";
	private Bitmap imageBitmap;
	private String title="相片选择";
	private Uri uri_old=Uri.parse("file://" + "/" + Environment.getExternalStorageDirectory().getPath() + "/"+ ComnConfig.ImageFrameCacheDir + IMAGE_FILE_NAME);
	private Uri uri_new;
	
	@Override
	public boolean setTitleIsShow() {
		return false;
	}
	
	@Override
	protected void onCreateBase() {
        StatusBarUtil.setTransparentForImageView(this,null);
		title=getIntent().getStringExtra("title");
		if(Util.isEmpty(title)){
			title="相片选择";
		}
		setContentView(R.layout.ui_ximageupload);
		
		if(getIntent().getExtras().getInt("width") > 0){
			width=getIntent().getExtras().getInt("width");
		}
		
		if(getIntent().getExtras().getInt("height") > 0){
			height=getIntent().getExtras().getInt("height");
		}
		
		
		//进入动画
		into=AnimationUtils.loadAnimation(this, R.anim.t_view_ximageupload_into);
		//退出动画
		remove=AnimationUtils.loadAnimation(this, R.anim.t_view_ximageupload_remove);
		remove.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {}
			@Override
			public void onAnimationRepeat(Animation animation) {}
			@Override
			public void onAnimationEnd(Animation animation) {
				finish();
				//返回上一个activity动画public_activity_remove(这里这么写主要为了让这个activity看起来更像Dialog的形式)
				overridePendingTransition(R.anim.a_comn_ui_into,R.anim.a_comn_ui_remove);
			}
		});
		context=(LinearLayout) findViewById(R.id.pop_context);//选项卡集合
		context.startAnimation(into);
		TextView album = (TextView) findViewById(R.id.album);//相册选项卡
		TextView photograph = (TextView) findViewById(R.id.photograph);//拍照选项卡
		TextView cancel = (TextView) findViewById(R.id.cancel);//取消选项卡
		album.setOnClickListener(this);
		photograph.setOnClickListener(this);
		cancel.setOnClickListener(this);	
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.photograph: // 拍照
			File file=new File(Environment.getExternalStorageDirectory(), "/"+ ComnConfig.ImageFrameCacheDir+IMAGE_FILE_NAME);
			if (!file.getParentFile().exists())file.getParentFile().mkdirs();
			uri_new = FileUtil.createFileProviderUri(file.getPath());//通过FileProvider创建一个content类型的Uri
			Intent intent = new Intent();
			intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
			intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//设置Action为拍照
			intent.putExtra(MediaStore.EXTRA_OUTPUT, uri_new);//将拍取的照片保存到指定URI
			startActivityForResult(intent,1);
			break;
		case R.id.album: // 相册选取
			Intent intent1 = new Intent(Intent.ACTION_PICK); 
			intent1.setType("image/*");
			startActivityForResult(intent1, 2);  
			break;
		case R.id.cancel: // 取消
			context.startAnimation(remove);
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case 1:// 拍照
			if(resultCode == -1){//-1:确定 0:取消
				//需要再调用一次剪裁图片剪裁一到
				 if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
				    	//sdk>=24
				    	startPhotoZoom(uri_new);
				 }else 
				 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT&&android.os.Build.VERSION.SDK_INT<Build.VERSION_CODES.N) {
				    	//19=<sdk<24
				    	startPhotoZoom(uri_old);
				 }else {
				    	//sdk<19
				    	startPhotoZoom(uri_old);
				 }
			}
			break;
		case 2:// 相册
			if(resultCode == -1){//-1:确定 0:取消
				//需要再调用一次剪裁图片剪裁一到
				startPhotoZoom(data.getData());
			}
			break;
		case 3:// 裁剪图片
			if (data != null) {
				setResult(1, new Intent().setData(uri_old));
				finish();
			}
			break;
		}
	}

	//裁剪图片方法实现(这里只有相机使用,相册自带有剪裁不需要)
	public void startPhotoZoom(Uri uri) {
	    Intent intent = new Intent("com.android.camera.action.CROP");
	    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {        
	      intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);    
	    }   
	    intent.setDataAndType(uri, "image/*");
	    // 设置裁剪
	    intent.putExtra("crop", "true");
	    // aspectX aspectY 是宽高的比例
	    intent.putExtra("aspectX", width);
	    intent.putExtra("aspectY", height);
	    intent.putExtra("scale", true); 
	    // outputX outputY 是裁剪图片宽高
	    intent.putExtra("outputX", width);
	    intent.putExtra("outputY", height);
	    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri_old);
	    intent.putExtra("return-data", false);
	    startActivityForResult(intent, 3);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK ) {  
			context.startAnimation(remove);
        }  
        return false; 
	}
}
